package com.fw.application.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.UserController;
import com.fw.common.Builder;
import com.fw.common.IdXD;
import com.fw.enums.CodeStatus;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.*;
import com.fw.system.web.model.vo.CityCodeVo;
import com.fw.system.web.model.vo.UserMapVo;
import com.fw.system.web.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 城市战队字典表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/web/city")
@Api(tags = "城市列表相关")
public class TbCityCodeController implements UserController {

    @Autowired
    private ITbCityCodeService cityCodeService;
    @Autowired
    private ITbUserCityJoinService userCityJoin;
    @Autowired
    private IdXD idXD;
    @Autowired
    private ITbSysRuleService ruleService;
    @Autowired
    private ITbUserService userService;
    @Autowired
    private ITbFriendsService friendsService;

    @GetMapping("/getList")
    @ApiOperation("查询城市战队列表")
    public Result<List<TbCityCode>> getList() {
        List<TbCityCode> list = cityCodeService.list(Wrappers.<TbCityCode>lambdaQuery().orderByAsc(TbCityCode::getSortCode));
        return success(list);
    }

    @GetMapping("/getBySortCode")
    @ApiOperation("根据sortCode查询城市战队信息")
    public Result getBySortCode(@RequestParam String sortCode) {
        return success(cityCodeService.getOne(Wrappers.<TbCityCode>lambdaQuery().eq(TbCityCode::getSortCode, sortCode)));
    }


    @GetMapping("/selectCity")
    @ApiOperation("选择战队,参数：id魏战队城市的id")
    public Result selectCity(@RequestParam String id) {
        String userId = this.getUser().getId();
        TbSysRule startAndEndTime = ruleService.getById(8);

        if (LocalDateTime.now().compareTo(startAndEndTime.getStartTime()) < 0)
            return new Result().fail(500, "活动还未开始");
        Assert.isNull(userCityJoin.getOne(Wrappers.<TbUserCityJoin>lambdaQuery().eq
                (TbUserCityJoin::getUserId, userId)), CodeStatus.USER_CITY_EXIST.getMessage());
        Builder.of(TbUserCityJoin::new)
                .with(TbUserCityJoin::setId, idXD.nextId())
                .with(TbUserCityJoin::setCityCode, id)
                .with(TbUserCityJoin::setUserId, userId)
                .with(TbUserCityJoin::setIsType, 1)
                .with(TbUserCityJoin::setCreateTime, LocalDateTime.now()).build().insert();
        this.getUser().setCityCode(id).updateById();
        return success();
    }


    @GetMapping("/getUserMap")
    @ApiOperation("首页查询好友解锁情况")
    public Result getUserMap() {
        String userId = this.getUser().getId();
        TbCityCode nowUserCityCode = cityCodeService.getOne(Wrappers.<TbCityCode>lambdaQuery().eq(TbCityCode::getSortCode, this.getUser().getNowCityCount()));
        //当前用户解锁情况
        UserMapVo userMapVo = Builder.of(UserMapVo::new)
                .with(UserMapVo::setUserId, userId)
                .with(UserMapVo::setNickName, this.getUser().getNickName())
                .with(UserMapVo::setImgUrl, this.getUser().getImgUrl())
                .with(UserMapVo::setSortCode, nowUserCityCode.getSortCode())
                .with(UserMapVo::setCityName, nowUserCityCode.getCityName())
                .with(UserMapVo::setCityImg, nowUserCityCode.getCityImg()).build();
        List<String> collect = friendsService.list(Wrappers.<TbFriends>lambdaQuery().eq(TbFriends::getUserId, userId)).stream().map(TbFriends::getFriendsId).collect(Collectors.toList());
        List<CityCodeVo> cityCodeVoList = cityCodeService.list().stream().map(map -> {
            CityCodeVo build = Builder.of(CityCodeVo::new).build();
            BeanUtil.copyProperties(map, build);
            List<TbUser> tbUsers = null;
            if (collect.size() > 0) {
                tbUsers = userService.list(Wrappers.<TbUser>lambdaQuery().eq(TbUser::getNowCityCount, build.getSortCode()).in(TbUser::getId, collect).last("limit 3"));
            }
            build.setUsers(tbUsers);
            return build;
        }).sorted(Comparator.comparing(CityCodeVo::getSortCode)).collect(Collectors.toList());
        userMapVo.setCityCodes(cityCodeVoList);
        return success(userMapVo);
    }

}

